﻿using Common.Base;
using HandyControl.Data;
using Models;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Xml.Linq;

namespace SanmaoSystem.ViewModels
{
    public class RecordViewModel :BindableBase

    {   private string _number="";
        public string Number { get { return _number; } set { SetProperty(ref _number, value); } }
        private string _tag="";
        public string Tag { get { return _tag; } set { SetProperty(ref _tag, value); } }
        private string _cargoName="";
        public string CargoName { get { return _cargoName; } set { SetProperty(ref _cargoName, value); } }
        private string _id="";
        public string Id { get { return _id; } set { SetProperty(ref _id, value); } }
        private string _dataListShow = "Visible";
        public   string DataListShow { get { return _dataListShow; } set { SetProperty(ref _dataListShow, value); } }
        private readonly IRegionManager _regionManager;
        //模糊查询关键词
        private string _keyWord;
        public string KeyWord { get { return _keyWord; } set { SetProperty(ref _keyWord,value); } }
        //分页总页数
        private int _totalPage;
        public int TotalPage { get { return _totalPage; } set { SetProperty(ref _totalPage, value); } }
        //当前页码
        private int _pageIndex=1;
        //每页显示数据
        private int PageSize = 5;
        public List<Record> list;
        public int PageIndex { get { return _pageIndex; } set { SetProperty(ref _pageIndex, value); } }
        //换页命令
        public DelegateCommand<FunctionEventArgs<int>> PageUpdatedCmd { get => new DelegateCommand<FunctionEventArgs<int>>(Update); }
        public ObservableCollection<Record> DataList { set; get; }
        //删除
        private DelegateCommand<int> _delete;
        public DelegateCommand<object> Delete { get => new DelegateCommand<object>(DeleteById);  }
      
        //编辑
        public DelegateCommand<object> Edit { get => new DelegateCommand<object>(OpenEdit); }

        private void OpenEdit(object obj)
        {
           RecordIProvider recordIProvider = new RecordIProvider();
            int id = (int)obj;
            Record record = recordIProvider.GetById(id);
            CargoName = record.CargoName;
            Tag =  record.Tag;
            Id = record.Id.ToString();
            Number = record.Number.ToString();
            DataListShow = "Hidden";
        }

        //添加
        public DelegateCommand Insert { get => new DelegateCommand(OpenInsertCargoType);  }

        private void OpenInsertCargoType()
        {
            CargoName = "";
            Tag = "";
            Number = "";
            DataListShow = "Hidden";
        }

        //编辑/保存
        public DelegateCommand Save1 { get => new DelegateCommand(SaveEdit);  }

        private void SaveEdit()
        {
            RecordIProvider recordIProvider=new RecordIProvider();
            Record record =new Record();
            if (Id == "")
            {
                record.CargoName = CargoName;
                record.Tag = Tag;
                record.Number=float.Parse(Number);
                record.MemberName=(string) Application.Current.Properties["UserName"];
                record.MemberId = 0;
                record.InsertDate = DateTime.Now;
                recordIProvider.Insert(record);
                CargoName = "";
                Tag = "";
                Number = "";
                Id = "";
                MessageBox.Show("添加成功！");
            }
            else
            {
                record.Id=int.Parse(Id);
                record.CargoName = CargoName; record.Tag = Tag;
                record.Number=float.Parse(Number);
                recordIProvider.Update(record);
                 CargoName = "";
                Tag = "";
                Number = "";
                Id = "";
                MessageBox.Show("修改成功！");
            }
               DataListShow = "Visible";
              Refresh(recordIProvider);
        }

        //取消
        public DelegateCommand Cancel { get => new DelegateCommand(Cancel1);  }

        private void Cancel1()
        {
            CargoName = "";
             Tag = "";
             Id = "";
            Number = "";
             DataListShow = "Visible";
        }

        private void DeleteById(object obj)
        {
             int id = (int)obj;
            RecordIProvider recordIProvider = new RecordIProvider();
            recordIProvider.Delete(id);
            Refresh(recordIProvider);
        }

        public RecordViewModel()
        {
             DataList = new ObservableCollection<Record>();

            Reload();
        }
           //刷新数据
        public void Refresh(RecordIProvider recordIProvider )
        {
            list = recordIProvider.GetAll();
            PageIndex = 1;
            int start=(PageIndex-1)* PageSize;
            int end= Math.Min(start+PageSize,list.Count);
            GetList(start, end);
        }
          public void Reload()
        {
             RecordIProvider recordIProvider = new RecordIProvider();
             list = recordIProvider.GetAll();
            //总页数 = (总数据量 + 每页数据量 - 1) / 每页数据量
            TotalPage = (list.Count+PageSize-1)/ PageSize;
            int start=(PageIndex-1)* PageSize;
            int end= Math.Min(start+PageSize,list.Count);
            GetList(start, end);
        }
        public void Update(FunctionEventArgs<int> pageIndex )
        {  
            int start=(pageIndex.Info-1)*PageSize;
            int end= Math.Min(start+PageSize,list.Count);
            GetList(start, end);
        }

        public void GetList( int start,int end)
        {

            DataList.Clear();
            var ListNow = list.GetRange(start, end-start);
            foreach(var item in ListNow)
            {
                DataList.Add(item);
            }
        }
    }
}
